﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using jnxceq.Common.Log;
using System.Configuration;

namespace jnxceq.Common.Net
{
    public class Connetion
    {
        Form1 fmain = null;
        //监听服务
        Socket socketSever;
        Thread threadSocketListen;
        Form1 frmMain;
        public static int Clientsum = 0;
        #region 获取本地IP地址
        //提供一个IP地址
        IPAddress ipAddress = IPAddress.Any;
        //获取与主机关联的IP地址的列表
        IPAddress[] addressip = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
        public IPAddress GetIp()
        {
            try
            {
                foreach (IPAddress ips in addressip)
                {
                    if (ips.AddressFamily == AddressFamily.InterNetwork)
                    {
                        ipAddress = ips;
                        break;
                    }
                }
            }
            catch (Exception ex)
            {

                WFLog.Write(ex);
            }
            return ipAddress;
        }
        #endregion

        //启动监听线程
        public void Start(Form1 frm)
        {
            try
            {
                this.frmMain = frm;
                threadSocketListen = new Thread(StartListen);
                threadSocketListen.IsBackground = true;
                threadSocketListen.Start();
            }
            catch (Exception ex)
            {

                WFLog.Write(ex);
            }
        }
        //启动监听线程
        public void Start()
        {
            try
            {
                threadSocketListen = new Thread(StartListen);
                threadSocketListen.IsBackground = true;
                threadSocketListen.Start();
            }
            catch (Exception ex)
            {

                WFLog.Write(ex);
            }
        }

        //建立监听服务
        public void StartListen()
        {
            try
            {

                string IP = ConfigurationManager.AppSettings["IP"].ToString();
                int port = Convert.ToInt32(ConfigurationManager.AppSettings["Port"]);
                //创建端点
                IPEndPoint ipend = new IPEndPoint(IPAddress.Parse(IP), port);
                //创建服务器
                socketSever = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //端点绑定
                socketSever.Bind(ipend);
                //监听
                socketSever.Listen(100);
                WFLog.Write("服务开启" + ipend);
                Form1.DdisConn("监听开始：" + IP + ":" + port);
                
                Thread thread = new Thread(AcceptInfo);

                thread.IsBackground = true;

                thread.Start(socketSever);

            }
            catch (Exception ex)
            {

                WFLog.Write(ex);
            }
        }
        public void AcceptInfo(object o)
        {
            Socket socket = o as Socket;
            while (true)
            {
                try
                {
                    //创建通信用的Socket
                    Socket tSocket = socket.Accept();
                    //为新连接创建新的Socket                    
                    //WFLog.Write("客户端已连接" + ControlConnection.socketClient.RemoteEndPoint.ToString());
                    Form1.DdisConn("客户端已连接" + tSocket.RemoteEndPoint.ToString());
                    ControlConnection con = new ControlConnection();
                    con.start(tSocket);

                }

                catch (Exception ex)
                {
                    Form1.DdisConn(ex.Message);
                    break;

                }
            }

        }

    }
}